package com.iflytek.autoCompareTool.model;


import com.iflytek.autoCompareTool.common.Constants;
import com.iflytek.autoCompareTool.common.DBManager;
import com.iflytek.autoCompareTool.utils.ExcelUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.*;

/**
 * 数据库中一个FileName对应的所有测试集及相关信息
 *
 * @author llchen12
 * @date 2018/6/4
 */
public class TestData {

    private InputInfo inputInfo;

    private Map<String, List<TestSet>> testSetMap;

    public TestData(InputInfo inputInfo, Map<String, List<TestSet>> testSetMap) {
        this.inputInfo = inputInfo;
        this.testSetMap = testSetMap;
    }

    public TestData() {
    }

    public InputInfo getInputInfo() {
        return inputInfo;
    }

    public void setInputInfo(InputInfo inputInfo) {
        this.inputInfo = inputInfo;
    }

    public Map<String, List<TestSet>> getTestSetMap() {
        return testSetMap;
    }

    public void setTestSetMap(Map<String, List<TestSet>> testSetMap) {
        this.testSetMap = testSetMap;
    }

    public int size() {
        int size = 0;
        if (testSetMap != null) {
            for (List<TestSet> testSetList : testSetMap.values()) {
                size += testSetList.size();
            }
        }
        return size;
    }


    public static Map<String, List<TestSet>> parseInput(InputInfo input) {

        Map<String, List<TestSet>> testSetMap = new LinkedHashMap<>();
        String[] filePaths;
        String filePath = input.getFilePath();

        if (filePath.contains(";")) {
            filePaths = StringUtils.split(filePath, ";");
        } else {
            filePaths = new String[]{filePath};
        }

        String fileType = input.getFileType();
        Integer id = input.getId();
        List<TestSet> testSetList;
        for (String file : filePaths) {
            //文件上传测试集处理
            if (StringUtils.equals(fileType, Constants.CUSTOM_TEST)) {
                String filename = file.substring(file.indexOf("(") + 1,
                        file.lastIndexOf(")"));

                if (StringUtils.equals(input.getBaseEnv(), "expect") && input.getSingleEnv() == 0) {
                    //标注集对比
                    testSetList = ExcelUtil.readCustomTestSet(file, 1, filename, id);
                } else {
                    testSetList = ExcelUtil.readCustomTestSet(file, 0, filename, id);
                }
                testSetMap.put(filename, testSetList);
            } else if (StringUtils.equals(input.getFileType(), Constants.TEST_SET)) {
                String[] dataInput = file.split("@");
                String appid = dataInput[0];
                String company = dataInput[1];

                String sql = "select amtt.appid,amtt.usr_id usrId,amtt.user_params userParams,amtt.uid,amtt.text,amtt.service,amtt.semantic,amtt.operation,amtt.answer " +
                        "from aiui_manager.tool_testset amtt left join " +
                        "(select category from " +
                        "j_contents.app_config_mst jcacm " +
                        "where jcacm.appid=? ) as acm " +
                        "on amtt.service=acm.category " +
                        "where amtt.usr_id=?";


                Object[] args;
                if (!StringUtils.equals(company, "aiui_demo")) {
                    sql += " or amtt.usr_id=?";
                    args = new Object[]{appid, "aiui_demo", company};
                } else {
                    args = new Object[]{appid, "aiui_demo"};
                }

                QueryRunner qr = DBManager.getInstance().getAiuiManagerQr();
                try {
                    testSetList = qr.query(sql, new BeanListHandler<>(TestSet.class), args);
                } catch (SQLException e) {
                    e.printStackTrace();
                    testSetList = Collections.emptyList();
                }
                String fp = file + ".xlsx";
                testSetList.forEach(ts -> ts.setFileName(fp));
                testSetMap.put(fp, testSetList);
            }

        }
        return testSetMap;
    }



}
